package com.zjg.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zjg.dao.OnlineDao;
import com.zjg.dao.impl.OnlineDaoImpl;
import com.common.entity.A_adminer;
import com.common.entity.A_online;
import com.common.entity.A_opinion;
import com.common.utils.StringUtils;
import com.lyk.dao.A_adminerDao;
import com.lyk.dao.impl.A_adminerDaoImpl;

public class OnlineServlet extends HttpServlet {
	private OnlineDao onlineDao=new OnlineDaoImpl();
	private A_adminerDao admDao=new A_adminerDaoImpl();
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String opr=request.getParameter("opr");
		
		if("add".equals(opr)){
			add(request, resp);
		}else if("update".equals(opr)){
			update(request, resp);
		}
		else if("del".equals(opr)){
			delete(request, resp);
		}else{
			list(request, resp);
		}
		
		
	}
	

	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		String is_online=request.getParameter("is_online");
		String personal=request.getParameter("personal");
		Map<String, Object> map=new HashMap<String, Object>();
		List<A_online> listAll=onlineDao.findByCondition(null);
		List<A_online> listyes=new ArrayList<A_online>();
		List<A_online> listno=new ArrayList<A_online>();
		List<A_online> list=new ArrayList<A_online>();
		A_adminer adminer=(A_adminer) request.getSession().getAttribute("userInfo");
		if(personal!=null&&personal.length()>0)
		{
			map.put("admin_id",adminer.getAdmin_id());
			list=onlineDao.findByCondition(map);
		}
		else if(is_online!=null&&is_online.length()>0)
		{
			for(A_online online:listAll)
			{
				if(online.getEnd_time()==null) {listyes.add(online);}
				else {listno.add(online);}
			}
			list=is_online.equals("yes")?listyes:listno;
		}
		else
		{
			list=listAll;
		}
		
		request.setAttribute("onlinelist", list);
		
		String is_excute_online=(String) request.getSession().getAttribute("is_excute_online");
		if(is_excute_online!=null&&is_excute_online.equals("yes"))
		{
			request.getSession().setAttribute("is_excute_online", "no");
			request.getRequestDispatcher("online_list.jsp").forward(request, resp);
		}
		else
		{
			request.getSession().setAttribute("excuteMsg_online", "");
			request.getRequestDispatcher("online_list.jsp").forward(request, resp);
		}
	}
	
	private void update(HttpServletRequest request, HttpServletResponse resp)
			throws UnsupportedEncodingException, IOException {
		//2、判断数据的有效性
		//3、获取数据
		int rows=0;
		String errMsg="";
		A_adminer adminer=(A_adminer) request.getSession().getAttribute("userInfo");
		try{
			Integer online_id=(Integer) request.getSession().getAttribute("online_id");
			A_online a_online=null;
			if(online_id!=null){
				a_online=onlineDao.findById(online_id);
				if(a_online==null) throw new RuntimeException("找不到该记录："+online_id);
				//4、保存数据
				a_online.setEnd_time(new Timestamp(new Date().getTime()));
				rows=onlineDao.updateA_online(a_online);
			}else{
				throw new RuntimeException("不存在该记录");
			}
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		PrintWriter out=resp.getWriter();
		if(rows>0){
			out.println("<script type='text/javascript'>alert('退出成功');location.href='../login.jsp';</script>");
		}else{
			out.println("<script type='text/javascript'>alert('退出失败:"+errMsg+"');history.back();</script>");
		}
	}

	

	private void add(HttpServletRequest request, HttpServletResponse resp)
			throws IOException {
		//2、判断数据的有效性
		//3、获取数据
		A_online a_online=new A_online();
		int rows=0;
		String errMsg="";
		A_adminer adminer=(A_adminer) request.getSession().getAttribute("userInfo");
		String browser=(String) request.getSession().getAttribute("browser");
		String actip=(String) request.getSession().getAttribute("actip");
		try{
			a_online.setAdmin_id(adminer.getAdmin_id());
			a_online.setStart_time(new Timestamp(new Date().getTime()));
			a_online.setBrowser(browser);
			a_online.setActip(actip);
			//4、保存数据
			Object[] results=(Object[]) onlineDao.saveA_online(a_online);
			rows=(Integer) results[0];
			request.getSession().setAttribute("online_id", (Integer)results[1]);
		}catch(Exception e){
			e.printStackTrace();
			errMsg=e.getMessage();
		}
		PrintWriter out=resp.getWriter();
		if(rows>0){
			//out.println("<script type='text/javascript'>alert('新增成功');location.href='A_onlineServlet';</script>");
			resp.sendRedirect("./auction_list.jsp");
		}else{
			out.println("<script type='text/javascript'>alert('登陆失败:"+errMsg+"');history.back();</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse resp)
			throws IOException, ServletException {
		int rows=0;
		String errMsg="";
		
		String del_list=request.getParameter("del_list");
		if(del_list!=null&&del_list.length()>0)
		{
			String[] delArrStr=del_list.split(";");
			List<Integer> delArrInt=new ArrayList<Integer>();
			for(int i=0;i<delArrStr.length;i++)
			{
				delArrInt.add(StringUtils.str2Integer(delArrStr[i], null));
			}
			for(Integer i:delArrInt)
			{
				rows=onlineDao.deleteA_online(i);
			}
		}
		else{
			try{
				Integer online_id=StringUtils.str2Integer(request.getParameter("online_id"), null);
				if(online_id!=null){
					//4、保存数据
					rows=onlineDao.deleteA_online(online_id);
				}else{
					throw new RuntimeException("该记录不存在");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg=e.getMessage();
			}
		}
		if(rows>0){
			request.getSession().setAttribute("is_excute_online", "yes");
			request.getSession().setAttribute("excuteMsg_online", "删除成功");
		}else{
			request.getSession().setAttribute("excuteMsg_online", errMsg);
		}
		resp.sendRedirect("onlineServlet");
	}
	
}
